<?php
namespace Admin\Controller;
use Think\Controller;

class PhotosController extends CommonController{


	public function getlist(){
		$artid = $_GET['id'];
			
		$art_m = M('Article');
		//获取3列以上时，见开发手册
		$art = $art_m->where('id='.$artid)->getField('id,title,flag');
		$this->assign('art',$art[$artid]);
			
		$this->assign('flagname',$this->flag[$art[$artid]['flag']]);
		$this->assign('flagurl','/flag/'.$art[$artid]['flag']);
			
		$photo = M('Photos');
		$photolist = $photo->where('is_small = 0 and photoable_id='.$artid)->order('created_at desc')->select();
		$this->assign('data',$photolist);
		$this->display('Photos/list');
	}


	public function uploadimg(){
			
		$upload = new \Think\Upload();
		$upload->maxSize   =     3145728 ;
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
		$upload->savePath  =      '/images/';
		// 上传文件   js使用formdata 方式上传,formdata还真是 好用 啊
		$info   =   $upload->uploadOne($_FILES['upload_file']);
		if(!$info) {// 上传错误提示错误信息
			$this->show('error');
		}else{// 上传成功 获取上传文件信息
			$path = '/Uploads'.$info['savepath'].$info['savename'];
			$m = D('Photos');
			$_POST['path'] = $path;
			$_POST['is_small'] = 0;
			$_POST['parent_id'] = 0;
			if(!$m->create()){
				$this->show('error2');
			}
			$id = $m->add();
			if($id>0){
				$result['id'] = $id;
				if($_POST['is_editer']){
					$result['path'] = __ROOT__.$path;
				}else {
					$result['path'] =$path;
				}
					
				$this->ajaxReturn($result);
				//$this->show($id);
			}else{
				$this->show('0');
			}
		}
			
			
	}

	public function setsmall(){
		$id = $_POST['id'];//根据 ID 是否存在 来判断 是 添加还是 修改
		$upload = new \Think\Upload();
		$upload->maxSize   =     3145728 ;
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
		$upload->savePath  =      '/images/';
		$m=D(CONTROLLER_NAME);	
		if($id != null){
			$info   =   $upload->uploadOne($_FILES['fileselect']);

			if(!$info) {// 上传错误提示错误信息
				unset($_POST['path']);
			}else{// 上传成功 获取上传文件信息
				$_POST['path'] = '/Uploads'.$info['savepath'].$info['savename'];
					
				$imginfo = $m-> where('id='.$_POST['id'])->getField('path');
				//删除 原来的 图片
				if(is_file(__ROOT__.$imginfo)) {
					//echo "删除 员图片 ";
					unlink(__ROOT__.$imginfo);
				}
			}

			if(!$m->create()){
				$this->show('error');
			}

			$idNum=$m->save();
			if($idNum>0){
				$this->show('ok');
			}else{
				$this->show('error');
			}

		}else{
			$info   =   $upload->uploadOne($_FILES['fileselect']);
			if(!$info) {// 上传错误提示错误信息
			    $this->show('error');
			}else{// 上传成功 获取上传文件信息
			    $_POST['path'] = '/Uploads'.$info['savepath'].$info['savename'];
				if(!$m->create()){
					$this->error($m->getError());
				}
				
				$idNum=$m->add();
				if($idNum>0){
					$this->show('ok');
				}else{
					$this->show('error');
				}
				
			}


		}
	}


	public function small(){
		$artid = $_GET['artid'];
		$photoid = $_GET['photoid'];
			
		$art_m = M('Article');
		//获取3列以上时，见开发手册
		$art = $art_m->where('id='.$artid)->getField('id,title,flag');
		$this->assign('art',$art[$artid]);
			
		$photo = M('Photos');
		$originphoto = $photo->find($photoid);
		$small = $photo ->where('is_small=1 and parent_id ='.$photoid)->find();
			
			
		$this->assign('originphoto',$originphoto);
		$this->assign('small',$small);
			
		$this->display('Photos/small');
	}

}
?>
